//
//  SXCollectionTableViewController.m
//  ShiXun
//
//  Created by lanou on 15/11/3.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXCollectionTableViewController.h"
#import "SXCollectionManager.h"
#import "SXReadRecommendDetailViewController.h"
#import "SXVideoDetailViewController.h"
#import "SXVideoDetailModel.h"
#import "SXTopicDetailsViewController.h"

@interface SXCollectionTableViewController ()

@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;

@end

@implementation SXCollectionTableViewController

- (NSMutableArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = [NSMutableArray arrayWithObjects:@"阅读收藏",@"话题收藏",@"视听收藏", nil];
    }
    return _titleArray;
}

- (NSMutableArray *)dataSourceArray
{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
        
        [_dataSourceArray addObject:[SXCollectionManager checkCollectionWithTableName:@"read"]];
        [_dataSourceArray addObject:[SXCollectionManager checkCollectionWithTableName:@"topic"]];
        [_dataSourceArray addObject:[SXCollectionManager checkCollectionWithTableName:@"video"]];
    }
    return _dataSourceArray;
}

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self.dataSourceArray removeAllObjects];
    
    [_dataSourceArray addObject:[SXCollectionManager checkCollectionWithTableName:@"read"]];
    [_dataSourceArray addObject:[SXCollectionManager checkCollectionWithTableName:@"topic"]];
    [_dataSourceArray addObject:[SXCollectionManager checkCollectionWithTableName:@"video"]];
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = @"我的收藏";
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"collectionCell"];
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.titleArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.titleArray[section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataSourceArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collectionCell" forIndexPath:indexPath];
    
    // Configure the cell...
    cell.textLabel.text = self.dataSourceArray[indexPath.section][indexPath.row][0];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            SXReadRecommendDetailViewController *readVC = [[SXReadRecommendDetailViewController alloc] init];
            readVC.docid = self.dataSourceArray[0][indexPath.row][1];
            [self.navigationController pushViewController:readVC animated:YES];
        }
            break;
        case 1:
        {
            SXTopicDetailsViewController *topicVC = [[SXTopicDetailsViewController alloc] init];
            topicVC.thread_id = self.dataSourceArray[1][indexPath.row][1];
            [self.navigationController pushViewController:topicVC animated:YES];
        }
            break;
        case 2:
        {
            SXVideoDetailViewController *videoVC = [[SXVideoDetailViewController alloc] init];
            NSData *dataName = self.dataSourceArray[2][indexPath.row][1];
            NSDictionary *dict = [self returnDictWithData:dataName key:self.dataSourceArray[2][indexPath.row][0]];
            SXVideoDetailModel *model = [[SXVideoDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            videoVC.detailModel = model;
            [self.navigationController pushViewController:videoVC animated:YES];
            
        }
            break;
            
        default:
            break;
    }
}

- (NSDictionary *)returnDictWithData:(NSData *)dataName key:(NSString *)key
{
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:dataName];
    NSDictionary *dict = [unarchiver decodeObjectForKey:key];
    return dict;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
